package com.codingday.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TODONew extends Activity {

	Button buttonMapView = null;
	Button buttonSave = null;
	EditText editTextTitle = null;
	EditText editTextDesc = null;
	
	TextView tv_hello = null;
	//TextView tv_locationResult = null;
	
	double latitude;
	double longitude;
	int radius;
	
	boolean editMode = false;
	
	TODO editTODO = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newtodo);
		
		latitude = 0.0;
		longitude = 0.0;
		radius = 0;
		
		buttonMapView = (Button) findViewById(R.id.ButtonSearchFromMap);
		buttonSave = (Button) findViewById(R.id.ButtonSave);
		editTextTitle = (EditText) findViewById(R.id.EditTextTitle);
		editTextDesc = (EditText) findViewById(R.id.EditTextDescription);
		//tv_locationResult = (TextView) findViewById(R.id.newtodo_location_result);
		tv_hello = (TextView) findViewById(R.id.new_todo_hello);
		
		Intent intent = getIntent();
		editMode = intent.getBooleanExtra("EditMode", false);
		if(editMode){
			Log.i("TODONew","Edit TODO");
			long ID = intent.getLongExtra("ID", -1);
			if(ID > -1){
				Log.i("TODONew","Edit TODO ID : " + ID);
				tv_hello.setText("Edit Your Quest!");
				editTODO = TODOListManager.getTODO(ID);
				editTextTitle.setText(editTODO.getTitle());
				editTextDesc.setText(editTODO.getDesc());
				latitude = editTODO.getLatitude();
				longitude = editTODO.getLongitude();
				radius = editTODO.getRadius();
			}else{
				// TODO: error handling
			}
		}else{
			Log.i("TODONew","Make new Quest");
			tv_hello.setText("Create New Quest!");
		}
		
		buttonSave.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
					saveTODO();
					// go back to list
	
			}
			
		});
		
		
		buttonMapView.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(TODONew.this, MapLocPicker.class);
				if(editMode == true ){
					intent.putExtra("EditMode",true);
					intent.putExtra("LATITUDE", editTODO.getLatitude());
					intent.putExtra("LONGITUDE", editTODO.getLongitude());
					intent.putExtra("RADIUS", editTODO.getRadius());
					
				}else if(latitude != 0.0 && longitude != 0.0){
					intent.putExtra("EditMode",true);
					intent.putExtra("LATITUDE", latitude);
					intent.putExtra("LONGITUDE", longitude);
					intent.putExtra("RADIUS", radius);					
				}else{
					
				}
				startActivityForResult(intent, 2);
			}
			
		});
		
	}
	
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("GeoAlarm"," Resume");
		//resetAll();
	}



	public void resetAll(){
		editTextTitle.setText("");
		editTextDesc.setText("");
		//buttonMapView.setText("Select Location");
		//tv_locationResult.setText("");
		buttonMapView.setText("Where?");
		latitude = 0.0;
		longitude = 0.0;
		radius = 0;
	}
	
	
	public boolean saveTODO(){
		boolean result = true;
		// TODO Auto-generated method stub
		// Save Here
		// Go Back To Prev Activity

		if(check()){
			TODO todo = null;
			if(editMode == true){
				todo = editTODO;
			}else{
				todo = new TODO();
			}
			Log.i("New TODO","TITLE : "+editTextTitle.getText().toString() + ":" + editTextTitle.getText().length());
			Log.i("New TODO","DESC  : "+editTextDesc.getText().toString() + ":" + editTextDesc.getText().length());

			todo.setTitle(editTextTitle.getText().toString());
			todo.setDesc(editTextDesc.getText().toString());
			todo.setLocation(latitude, longitude,radius);
			
			
			if(editMode==false){
				todo.checkCreatedInArea();
			}
			
			boolean saved = false;
			if(editMode == true){
				//saved = TODOListManager.
				saved = TODOListManager.saveEditTODO(todo);
			}else{
				saved = TODOListManager.addTODO(todo);
			}
			
			if(saved){
				Log.i("New TODO","Save Success");
			}else{
				Log.i("New TODO","Save Failed");
			}
		}else{
			Toast.makeText(getApplicationContext(), "Wrong Input. Please Check Again", 1).show();
			return false;
		}
		
		
		
		Log.i("New Todo", "Save Presssed");
		resetAll();
		if(editMode == true){
			finish();
		}else{
			GeoAlarm.getInstance().setCurrentTab(1);
		}
		Toast.makeText(getApplicationContext(), "Quest saved", 1).show();
		
		
		return result;
	}


	public boolean check(){
		Log.i("New ToDo","Check Inputs");
		//Log.i();
		Log.i("New TODO",editTextTitle.getText().toString());
		if(latitude == 0.0 && longitude == 0.0){
			return false;
		}
		if(editTextTitle.getText().length() == 0){
			return false;
		}
		if(radius <= 0){
			return false;
		}
		return true;
	}
	
	

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent result = new Intent();
		result.putExtra("result", false);
		setResult(Activity.RESULT_OK, result);				
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		switch(requestCode){
		case 2:
			this.latitude = data.getDoubleExtra("location_latitude", 0);
			this.longitude = data.getDoubleExtra("location_longitude", 0);
			this.radius = data.getIntExtra("location_radius", 0);
			buttonMapView.setText("OK!");
			//Toast.makeText(getApplicationContext(), "Map Pocked ("+latitude+","+longitude+")", 1).show();
			//tv_locationResult.setText("[ "+latitude+" , "+longitude+" ]");
			//buttonMapView.setText("[ "+latitude+" , "+longitude+" ]");
			
			
			break;
		default:
			Toast.makeText(getApplicationContext(), "Map Pocked default" + requestCode, 1).show();
			break;
		}
	}

	
	
}
